// FirstFixNextUnfix.cpp: implementation of the FirstFixNextUnfix class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "phsshare.h"
#include "FirstFixNextUnfix.h"
#include "JointPart.h"
#include "joint.h"
#include "JointFunction.h"
#include "Part.h"
#include "JointPortSource.h"
#include "JointPortFormer.h"
#include "PersistentFacade.h"

using namespace JointSystem;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

FirstFixNextUnfix::FirstFixNextUnfix()
{

}

FirstFixNextUnfix::~FirstFixNextUnfix()
{

}

BOOL FirstFixNextUnfix::Execute()
{
	BOOL bResult = FALSE;

/*	GeneralJointRegular* pRegular = static_cast<GeneralJointRegular*>(GetJointRegular());

	Part* pPart = pRegular->GetJoint()->GetPrev()->GetOwner();
	Part* pNextPart = pRegular->GetJoint()->GetNext()->GetOwner();
	JointPort* pFirstPort = pRegular->GetJoint()->GetPrev()->jointPort();
	ASSERT(pFirstPort);

	JointSystem::JointPortSource* pSource = pFirstPort->Source();
	JointSystem::JointPortFormer* pNextFormer = pRegular->m_pNextFormer;

	CString strRegular = GenerateRegular(pSource, pNextFormer);

	PartCrudeDataSet* pSet = GetPersistentFacade().GetPartCrudeDataSet();
	pSet->SetDataStandard(pNextPart->GetStandard());
	BOOL bResult = pSet->InstancePart(pNextPart, pNextPart->GetCustomID(), strRegular);
	pSet->Close();//*/

	return bResult;
}

CString FirstFixNextUnfix::GenerateRegular(JointPortSource *pFirstSource, JointPortFormer *pNextFormer)
{
	ASSERT(pFirstSource);
	ASSERT(pNextFormer);

	GeneralJointRegular* pRegular = static_cast<GeneralJointRegular*>(GetJointRegular());

	CString strRegular;
/*	CString strOrder = pRegular->GetPressField();
	if(pFirstSource->SizeRangeCount() != pNextFormer->SizeRangeCount())
		return CString();

	for(int i = 0; i < pFirstSource->SizeRangeCount(); i++)
	{
		Real<Set<double> >& real = pFirstSource->SizeRangeAt(i);
		RealTextExpSet& text = pNextFormer->SizeRangeAt(i);
		if(text.IsUnknown())
			continue;

		CStringArray* pstrArr = JointRegular::GetSizeRangeText(text);
		for(int j = 0; j < pstrArr->GetSize(); j++)
		{
			if(IsNumber(pstrArr->GetAt(j)))
				continue;

			strOrder += _T(",") + pstrArr->GetAt(j);
		}
		delete pstrArr;

		CString strTemp = GenerateOneRegular(real, text);
		if(strTemp.IsEmpty())
			continue;
		if(strRegular.IsEmpty())
		{
			strRegular += strTemp;
			continue;
		}
		strRegular += _T(" and ") + strTemp;
	}
	
	strRegular += strRegular.IsEmpty()?_T(""):_T(" and ")
		+ GenerateOneRegular() + _T(" order by ") + strOrder;
*/
	return strRegular;
}

CString FirstFixNextUnfix::GenerateOneRegular(Real<Set<double> >& real, JointSystem::RealTextExpSet& text)
{
	if(real.GetValue().GetCollection().GetSize() != 0 &&
		text.GetValue().GetExtentCount() != 0)
	{
		CString strRegular;
		strRegular.Format(_T(" ") + text.GetValue().GetExtentAt(0).m_etUpperBound.operator CString() +
			_T(">= %g and %g >= ") + 
			text.GetValue().GetExtentAt(0).m_etLowerBound.operator CString() + _T(" "), 
			real.GetValue().GetCollection().GetAt(0),
			real.GetValue().GetCollection().GetAt(0));
		return strRegular;
	}
	if(real.GetValue().GetExtentCount() != 0 && text.GetValue().GetCollection().GetElementCount() != 0)
	{
		CString strRegular;
		strRegular.Format(_T(" ") + text.GetValue().GetCollection().GetElementAt(0).operator CString()+
			_T(">= %g and %g >=") +
			text.GetValue().GetCollection().GetElementAt(0).operator CString() + _T(" "),
			real.GetValue().GetExtentAt(0).LowerBound().Value(),
			real.GetValue().GetExtentAt(0).UpperBound().Value());
		return strRegular;
	}
	if(real.GetValue().GetExtentCount() != 0 && text.GetValue().GetExtentCount() != 0)
	{
		CString strRegular;
		CString strLowerBound = text.GetValue().GetExtentAt(0).m_etLowerBound.operator CString();
		CString strUpperBound = text.GetValue().GetExtentAt(0).m_etUpperBound.operator CString();
		strRegular.Format(_T(" ((") + strLowerBound +
			_T("<= %g and %g <= ") + strUpperBound + _T(") or (") + strLowerBound +
			_T(">= %g and %g >= ") + strUpperBound + _T("))"),
			real.GetValue().GetExtentAt(0).LowerBound().Value(),
			real.GetValue().GetExtentAt(0).UpperBound().Value(),
			real.GetValue().GetExtentAt(0).LowerBound().Value(),
			real.GetValue().GetExtentAt(0).UpperBound().Value());
		return strRegular;
	}
	if(real.GetValue().GetCollection().GetSize() != 0 &&
		text.GetValue().GetCollection().GetElementCount() != 0)
	{
		CString strRegular;
		strRegular.Format(text.GetValue().GetCollection().GetElementAt(0).operator CString() + _T(" = %g"),
			real.GetValue().GetCollection().GetAt(0));
		return strRegular;
	}
	return CString();
}

CString FirstFixNextUnfix::GenerateOneRegular()
{
	GeneralJointRegular* pRegular = static_cast<GeneralJointRegular*>(GetJointRegular());
	CString strTemp(pRegular->GetPressField());

	if(strTemp.IsEmpty())
		return CString();

	CString strRegular;
	pRegular->GetJoint()->GetNext()->SetForce(pRegular->GetJoint()->GetPrev()->GetForce());
	strRegular.Format(_T("%s >= %g"), strTemp, pRegular->GetJoint()->GetPrev()->GetForce().length());

	return strRegular;
}